package thread;

import javax.swing.*;
import java.awt.*;

public class MillisTimer extends JFrame {
    public MillisTimer() {
        setTitle("秒表");
        setSize(300,300);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JLabel label=new JLabel("00:000");
        add(label);
        Font font=new Font("微软雅黑",Font.BOLD,72);
        label.setFont(font);
       new Thread(new Runnable() {//创建线程
           @Override
           public void run() {
               long s=0,ms, start=System.currentTimeMillis();
               while (true){
                   ms=System.currentTimeMillis()-start;
                   s=ms/1000;ms=ms%1000;
                       String ss=s+"",sms=ms+"";
                       if (s<10)ss="0"+ss;
                       if (ms<10)sms="00"+sms;
                       else if (ms<100) sms="0"+ms;
                       String str=ss+":"+sms;
                       label.setText(str);
                       System.out.println(str);
                   try {
                       Thread.sleep(200);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
       }).start();
    }
    public static void main(String[] agrs){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MillisTimer();
            }
        });
    }
}
